package com.wobangkj.common.service.impl;

import com.wobangkj.common.model.crud.Job;
import com.wobangkj.common.repository.JobRepository;
import com.wobangkj.common.service.JobService;
import com.wobangkj.tool.api.result.Result;
import com.wobangkj.tool.util.sql.CrudUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

import static com.wobangkj.tool.util.RestUtil.copyNonNullProperties;

@Service
@Slf4j
public class JobServiceImpl  implements JobService {
    @Autowired
    private JobRepository repository;

    @Override
    public Object search(Map<String, Object> params, Job data) {

        return CrudUtil.search(params, data, repository);
    }

    @Override
    public Object id(Long id) {
        return CrudUtil.getById(id, repository);
    }

    @Override
    public Object delete(Long id) {
        return CrudUtil.delete(id, repository);
    }
    @Override
    public Object update(Job job) {

        Job existData = repository.findById(job.getId()).get();
        copyNonNullProperties(job, existData);
        return CrudUtil.update(existData, repository);
    }

    @Override
    @SuppressWarnings("Duplicates")
    public Object create(Job job) {
        try {
            Job createData = repository.save(job);
            // 使用 uuid 作为源 token
            //String     token = UUID.randomUUID().toString().replace("-", "");
            //TokenModel model = new TokenModel(createData.getId(), token);
            // 30 分钟有效期
            //cacheManager.set(model.getToken(), new CacheModel(TOKEN_MINUTE, model));
            return Result.GetMapData(Result.CodeCreate, Result.MsgCreate);//, model);
        } catch (Exception e) {
            return Result.GetMapData(Result.CodeSql, e.getCause().getCause().getMessage());
        }
    }

    @Override
    public List<Job> findByNameLike(String job_name){return repository.findByNameLike(job_name);}


    @Override
    public List<Object> selectAllJobView(){return repository.selectAllJobView();}

    @Override
    public List<Map> list(){return repository.list();}

    @Override
    public List<Map> listByNum(){return repository.listByNum();}

    @Override
    public List<Map> idlist(long id){return repository.idlist(id);}

    @Override
    public List<Map> headimgList(long id) {
        return repository.headimgList(id);
    }

    @Override
    public List<Map> getListByName(String name){return repository.getListByName(name);}

    @Override
    public List<Map> list(String company_name,Integer company_num,Integer company_finance,Integer college,Integer major,Integer job_class,Integer job_class_second){
        return repository.list(company_name,company_num,company_finance,college,major,job_class,job_class_second);
    }

    @Transactional
    @Override
    public Object updateJobForward(Long id) {

        try {
            repository.updateJobForward(id);
            return Result.GetMapData(Result.CodeSuccess,Result.MsgSuccess);//, model);
        } catch (Exception e) {
            return Result.GetMapData(Result.CodeSql, e.getCause().getCause().getMessage());
        }
    }
}
